package com.ysd.lis.service.lab.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.lab.LabFeeItem;
import com.ysd.lis.entity.lab.LabInspectCompanyFeeitem;
import com.ysd.lis.entity.lab.LabInspectCompanyUser;
import com.ysd.lis.mapper.lab.LabInspectCompanyUserMapper;
import com.ysd.lis.request.lab.CompanyPageParam;
import com.ysd.lis.service.lab.LabInspectCompanyUserService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 送检单位对照用户 服务实现类
 * </p>
 *
 * @author WH
 * @since 2024-10-21
 */
@Service
public class LabInspectCompanyUserServiceImpl extends ServiceImpl<LabInspectCompanyUserMapper, LabInspectCompanyUser> implements LabInspectCompanyUserService {

    @Autowired
    private LabInspectCompanyUserMapper companyUserMapper;

    @Override
    public Result addCompanyUser(LabInspectCompanyUser companyUser) {
        int insert = companyUserMapper.insert(companyUser);
        if (insert > 0) {
            return Result.succ(1, "添加成功", companyUser);
        } else {
            return Result.fail(400, "添加失败", "");
        }
    }

    @Override
    public Result findInspectCompanyUserList(CompanyPageParam param) {
        MPJLambdaWrapper<LabInspectCompanyUser> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(LabInspectCompanyUser.class);
        queryWrapper.selectAs("u", SysUser::getRealname, LabInspectCompanyUser::getRealname);

        queryWrapper.leftJoin(SysUser.class, "u", p -> p.eq(SysUser::getUsername, LabInspectCompanyUser::getUsername).eq(LabFeeItem::getDelFlag, 0));
        queryWrapper.eq(LabInspectCompanyUser::getDelFlag, 0);
        if(ToolsUtils.isNotEmpty(param.getCompanyCode())){
            queryWrapper.eq(LabInspectCompanyUser::getCompanyCode, param.getCompanyCode());
        }
        if(ToolsUtils.isNotEmpty(param.getUsername())){
            queryWrapper.eq(LabInspectCompanyUser::getUsername, param.getUsername());
        }
        if (ToolsUtils.isNotEmpty(param.getSeachValue())) {
            queryWrapper.and(wrapper ->
                    wrapper.like(LabInspectCompanyUser::getUsername, param.getSeachValue())
                            .or()
                            .like(LabInspectCompanyUser::getRealname, param.getSeachValue())
            );
        }
        List<LabInspectCompanyUser> detailList = companyUserMapper.selectList(queryWrapper);

        return Result.succ(1, "查询成功", detailList);
    }

    @Override
    public Result batchDeleteCompanyUserList(List<LabInspectCompanyUser> list) {
        List<String> ids = list.stream().map(LabInspectCompanyUser::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);

        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }
}
